Pasteboard
The Pasteboard namespace provides a complete interface for reading, writing, and observing system pasteboard (clipboard) changes in the Scripting app.
Compared to the deprecated Clipboard API, Pasteboard offers more robust features, including:
- Support for multiple data types (text, images, URLs, binary data, etc.)
- Change event callbacks (
onChangedandonRemoved) - Privacy controls such as
localOnlyand expiration time
Note To allow pasting from other apps, go to: Settings > Scripting > Paste from Other Apps > Allow
Namespace: Pasteboard
Type Definition
Item
Represents a pasteboard item.
Each item is a map (Record<UTType, string | UIImage | Data>) where the key is a data type (UTType) and the value can be a string, image, or binary data.
Common types:
public.plain-text→ plain textpublic.url→ URL stringpublic.jpeg/public.png→ image (UIImage)public.data→ binary data (Data)
Example
Properties
hasStrings: Promise<boolean>
Checks whether the pasteboard contains text content.
Example
hasImages: Promise<boolean>
Checks whether the pasteboard contains images.
Example
hasURLs: Promise<boolean>
Checks whether the pasteboard contains URLs.
Example
numberOfItems: Promise<number>
Returns the number of items currently stored in the pasteboard.
Example
changeCount: Promise<number>
Returns the number of times the pasteboard contents have changed since system startup. This value increases whenever the pasteboard is modified (items added, updated, or removed).
Example
Text Operations
getString(): Promise<string | null>
Retrieves the text string of the first pasteboard item.
Example
setString(string: string | null): Promise<void>
Sets the text string of the first pasteboard item.
Example
getStrings(): Promise<string[] | null>
Retrieves all text strings from the pasteboard.
Example
setStrings(strings: string[] | null): Promise<void>
Sets multiple text strings to the pasteboard (each string becomes a separate item).
Example
URL Operations
getURL(): Promise<string | null>
Retrieves the first URL string from the pasteboard.
Example
setURL(url: string | null): Promise<void>
Sets a URL string as the first pasteboard item.
Example
getURLs(): Promise<string[] | null>
Retrieves all URL strings from the pasteboard.
Example
setURLs(urls: string[] | null): Promise<void>
Sets multiple URLs to the pasteboard.
Example
Image Operations
getImage(): Promise<UIImage | null>
Retrieves the first image (UIImage) from the pasteboard.
Example
setImage(image: UIImage | null): Promise<void>
Sets the first pasteboard item to an image.
Example
getImages(): Promise<UIImage[] | null>
Retrieves all image objects from the pasteboard.
Example
setImages(images: UIImage[] | null): Promise<void>
Sets multiple images to the pasteboard.
Example
Item Management
addItems(items: Item[]): Promise<void>
Appends new items to the existing pasteboard content without clearing it.
Example
setItems(items: Item[], options?: { localOnly?: boolean, expirationDate?: Date }): Promise<void>
Replaces the pasteboard contents with new items and applies optional privacy settings.
Parameters
items: An array of pasteboard items.options.localOnly: Iftrue, prevents the pasteboard content from being shared to other devices via Handoff.options.expirationDate: Sets an expiration time after which the system automatically removes the content.
Example
getItems(): Promise<Item[] | null>
Retrieves all pasteboard items as an array of Pasteboard.Item objects.
Example
Event Callbacks
onChanged: ((addedKeys: string[]) => void) | null | undefined
Called when the pasteboard content changes.
The parameter addedKeys is an array of the added representation types (UTType).
Example
onRemoved: ((removedKeys: string[]) => void) | null | undefined
Called when content is removed from the pasteboard.
The parameter removedKeys is an array of the removed representation types (UTType).
Example
Deprecated Clipboard API
The legacy Clipboard namespace is deprecated and retained only for backward compatibility.
Best Practices
- Always use the new
PasteboardAPI instead ofClipboard. - Use
changeCountto detect when pasteboard contents have changed. - Use
expirationDateto automatically clear sensitive data after a specific duration. - Set
localOnly: truefor data that should not sync across devices. - Use
hasStrings,hasImages, andhasURLsto check available content types before reading. - Use
onChangedandonRemovedcallbacks to react in real time to pasteboard updates.
